package mcchristian;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.DateCell;

public class Excel_Function {
	
	private List<News> newses;
	private List<Pray> prays;
	
	public Excel_Function() {
		
	}
 
	public List<News> contentNewsReading(String filePath) {
		WorkbookSettings ws = null;
		Workbook workbook = null;
		Sheet s = null;
		Cell rowData[] = null;
		FileInputStream fs = null;
		int rowCount = '0';
		int columnCount = '0';
 
		try {
			fs = new FileInputStream(new File(filePath));
			newses = new ArrayList<News>();
			ws = new WorkbookSettings();
			ws.setLocale(new Locale("en", "EN"));
			workbook = Workbook.getWorkbook(fs, ws);
 
			s = workbook.getSheet(0);
			rowCount = s.getRows();
			columnCount = s.getColumns();
 
			for (int i = 1; i < rowCount; i++) {
				String time = "";
				String content = "";
				rowData = s.getRow(i);
				if (rowData[0].getContents().length() != 0) { // the first date column must not null
					for (int j = 0; j < columnCount; j++) {
						switch (j) {
						case 0:
							time = rowData[j].getContents();
						case 1:
							content = rowData[j].getContents();
						default:
							break;
						}
					}
					newses.add(new News(time, content));
				}
			}
			workbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BiffException e) {
			e.printStackTrace();
		}
		return newses;
	}
	
	public List<Pray> contentPrayReading(String filePath) {
		WorkbookSettings ws = null;
		Workbook workbook = null;
		Sheet s = null;
		Cell rowData[] = null;
		FileInputStream fs = null;
		int rowCount = '0';
		int columnCount = '0';
 
		try {
			fs = new FileInputStream(new File(filePath));
			prays = new ArrayList<Pray>();
			ws = new WorkbookSettings();
			ws.setLocale(new Locale("en", "EN"));
			workbook = Workbook.getWorkbook(fs, ws);
 
			s = workbook.getSheet(0);
			rowCount = s.getRows();
			columnCount = s.getColumns();
 
			for (int i = 1; i < rowCount; i++) {
				String time = "";
				String content = "";
				rowData = s.getRow(i);
				if (rowData[0].getContents().length() != 0) { // the first date column must not null
					for (int j = 0; j < columnCount; j++) {
						switch (j) {
						case 0:
							time = rowData[j].getContents();
						case 1:
							content = rowData[j].getContents();
						default:
							break;
						}
					}
					prays.add(new Pray(time, content));
				}
			}
			workbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BiffException e) {
			e.printStackTrace();
		}
		return prays;
	}
}
